package com.yzdmw.toutiao.wheeldisk.ui;

import android.util.Log;
import android.view.View;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.OrientationHelper;
import androidx.recyclerview.widget.PagerSnapHelper;
import androidx.recyclerview.widget.RecyclerView;

import org.jetbrains.annotations.NotNull;

public class WheeldiskSnapHelper extends PagerSnapHelper {
    private static final String TAG = "WheeldiskSnapHelper";
    private static final boolean DEBUG = false;

    private OrientationHelper mVerticalHelper;
    private OrientationHelper mHorizontalHelper;
    private OrientationHelper getVerticalHelper(@NonNull RecyclerView.LayoutManager layoutManager) {
        if (mVerticalHelper == null) {
            mVerticalHelper = OrientationHelper.createVerticalHelper(layoutManager);
        }
        return mVerticalHelper;
    }
    private OrientationHelper getHorizontalHelper(@NonNull RecyclerView.LayoutManager layoutManager) {
        if (mHorizontalHelper == null) {
            mHorizontalHelper = OrientationHelper.createHorizontalHelper(layoutManager);
        }
        return mHorizontalHelper;
    }

    @Override
    public View findSnapView(RecyclerView.LayoutManager layoutManager) {
        OrientationHelper helperY = getVerticalHelper(layoutManager);
        OrientationHelper helperX = getHorizontalHelper(layoutManager);
        int childCount = layoutManager.getChildCount();
        if (childCount == 0) {
            return null;
        }

        View closestChild = null;
        final int center = helperY.getStartAfterPadding() + helperY.getTotalSpace() / 2;
        int childCenter = 0;
        int absClosest = Integer.MAX_VALUE;

        for (int i = 0 ; i < childCount ; i++) {
            final View child = layoutManager.getChildAt(i);
            // 直接跳过左边的
            if (helperX.getDecoratedStart(child) == 0) continue;

            childCenter = helperY.getDecoratedStart(child)
                    + (helperY.getDecoratedMeasurement(child) / 2);
            int absDistance = Math.abs(childCenter - center);

            // 找最靠近中心的
            if (absDistance < absClosest) {
                absClosest = absDistance;
                closestChild = child;
            } else
                break;
        }
        if (DEBUG) Log.d(TAG, "findCenterView center:" + center + ",target_center:" + childCenter);
        return closestChild;
    }

    @Override
    public int[] calculateDistanceToFinalSnap(RecyclerView.LayoutManager layoutManager, @NotNull View targetView) {
        int[] out = new int[2];
        if (layoutManager.canScrollVertically()) {
            OrientationHelper helper = getVerticalHelper(layoutManager);
            final int center = helper.getStartAfterPadding() + helper.getTotalSpace() / 2;
            int childCenter = helper.getDecoratedStart(targetView)
                    + (helper.getDecoratedMeasurement(targetView) / 2);
            out[1] = (int) ((childCenter - center) * 0.66);
            if (DEBUG) Log.d(TAG, "calculateDistanceToFinalSnap center:" + center + ",target_center:" + childCenter);
        }
        return out;
    }

}
